/*
    Copyright (C) 2010-2011 AmigaVR
    
    This file is part of AmigaVR.
    Written by Ketfi-Chérif Mehdi <ketficherif.mehdi@gmail.com>,
	       Laügt Franz <znarf94@gmail.com>

    AmigaVR is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AmigaVR is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AmigaVR.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef _DEFS_H
#define _DEFS_H

#ifndef TRUE
#define TRUE			1
#endif

#ifndef FALSE
#define FALSE			0
#endif

/* Sys Macro */
#define sys_set_rc32m()\
{\
	OSC.CTRL |= 0x02;\
	while ( !(OSC.STATUS&0x02) );\
	CCP = 0xD8;\
	CLK.CTRL = CLK_SCLKSEL_RC32M_gc;\
	OSC.CTRL = 0x02;\
}

/* DAC Macros */
#define dac_ready(dac,ch)(dac->STATUS&ch) 
#define dac_init(dac,dc,la)\
{\
	dac.CTRLA |= (dc)?0x0D:0x05;\
	dac.CTRLB |= (dc)?0x40:0x00;\
	dac.CTRLC |= (la)?0x01:0x00;\
	dac.CTRLC |= 0x08;\
}
#define dac_write(dac,data)(dac.CH0DATA = data)

/* FLASH Macros */
#define FLASH_CE 		0x00
#define FLASH_OE 		0x01
#define FLASH_WE 		0x02

/* RS232 Macros (deprecated) */
#define RS232_RTS 		0x03 // Sortie ->
#define RS232_TxD 		0x04 // Sortie ->
#define RS232_CTS 		0x05 // -> Entrée
#define RS232_RxD  		0x06 // -> Entrée
#define RS232_DELAY		833  // 1200 bauds
#define RS232_EOF		0x00
#define RS232_ERR_NOCTS		0x01
#define RS232_ERR_NOSTART	0x02
#define RS232_ERR_PARITY	0x03
#define rs232_get_rxd()		((PORTF.IN & _BV(RS232_RxD))>>RS232_RxD)
#define rs232_get_cts()		((PORTF.IN & _BV(RS232_CTS))>>RS232_CTS)
#define rs232_set_rts()		(PORTF.OUT |=   _BV(RS232_RTS))
#define rs232_unset_rts()	(PORTF.OUT &= ~(_BV(RS232_RTS)))

/* USART Macros */
#define USART			USARTC0
#define UPORT			PORTC
#define BSEL			12

/* Mod synth macro */
#define MOD_SONG_TITLE			0x0000
#define MOD_SONG_TITLE_SIZE		0x0014
#define MOD_SAMPLE			0x0014
#define MOD_SAMPLE_NAME			0x0000
#define MOD_SAMPLE_NAME_SIZE		0x0016
#define MOD_SAMPLE_LENGTH		0x0016
#define MOD_SAMPLE_FINETUNE		0x0018
#define MOD_SAMPLE_VOLUME		0x0019
#define MOD_SAMPLE_REPEAT_OFFSET	0x001A
#define MOD_SAMPLE_REPEAT_LENGTH	0x001C
#define MOD_NPATTERNS			0x03B6
#define MOD_JMP_END			0x03B7
#define MOD_PATTERN_TABLE		0x03B8
#define MOD_TAG				0x0438
#define MOD_TAG_SIZE			0x0004
#define MOD_NSAMPLES			0x001F
#define MOD_PATTERNS_DATA		0x043C
#define MOD_SAMPLES_DATA		0x083C
#define MOD_MAX_PATTERNS		0x80
#define MOD_SEQ				0x40
#define MOD_NCHN			0x04
#define MOD_NOT_SUPPORTED		0x00
#define MOD_SUPPORTED			0x01
#define MOD_SFX_ARPEGGIO		0x00
#define MOD_SFX_SLIDE_UP		0x01
#define MOD_SFX_SLIDE_DOWN		0X02
#define MOD_SFX_TONE_PORTAMENTO		0x03
#define MOD_SFX_VIBRATO			0x04
#define MOD_SFX_TONE_PORTAMENTO_VSLIDE	0x05
#define MOD_SFX_VIBRATO_VSLIDE		0x06
#define MOD_SFX_TREMOLO			0x07
#define MOD_SFX_SET_PAN_POS		0x08
#define MOD_SFX_SET_SAMPLE_OFFSET	0x09
#define MOD_SFX_VSLIDE			0x0A
#define MOD_SFX_POS_JUMP		0x0B
#define MOD_SFX_SET_VOLUME		0x0C
#define MOD_SFX_PATTERN_BREAK		0x0D
#define MOD_SFX_EX_EFFECTS		0x0E
#define MOD_SFX_SET_SPEED		0x0F
#define MOD_BUFFER_MAX			512
#define mod_get_sampleinfo(n,info)(MOD_SAMPLE+(n*30)+info)
#define mod_get_sample(data)((data&0x000000F0)+((data&0x00F00000)>>20))
#define mod_get_period(data)(((data&0x0000FF00)>>8)+(data&0x0000000F))
#define mod_get_args(data)(((data&0xFF000000)>>24)+((data&0x000F0000)>>16))


#define MAX_BUFFER			254

#endif
